import random
import string

class BankSystem:
    def __init__(self):
        # 管理员账号密码（固定）
        self.admin_account = "admin"
        self.admin_password = "12345"
        # 存储用户信息：卡号 -> {姓名、身份证、手机号、余额、密码、状态（正常/锁定）}
        self.user_data = {}
        # 已生成的卡号集合（避免重复）
        self.generated_cards = set()

    # 欢迎界面
    def show_welcome(self):
        print("*" * 50)
        print("***" + " " * 44 + "***")
        print("***" + " " * 18 + "欢迎登录银行管理系统" + " " * 18 + "***")
        print("***" + " " * 44 + "***")
        print("*" * 50)

    # 管理员登录
    def admin_login(self):
        print("请输入管理员账户：", end="")
        account = input().strip()
        print("请输入密码：", end="")
        password = input().strip()

        if account == self.admin_account and password == self.admin_password:
            print("操作成功，请稍后……")
            return True
        elif account != self.admin_account:
            print("管理员账户输入错误……")
            return False
        else:
            print("输入密码有误……")
            return False

    # 功能菜单界面
    def show_menu(self):
        print("=" * 50)
        print("*** 1.开户(1)                2.查询(2) ***")
        print("*** 3.取款(3)                4.存款(4) ***")
        print("*** 5.转账(5)                6.锁定(6) ***")
        print("*** 7.解锁(7)                          ***")
        print("***                                     ***")
        print("*** 退出(0)                             ***")
        print("=" * 50)

    # 生成6位不重复卡号
    def generate_card(self):
        while True:
            card = ''.join(random.sample(string.digits, 6))
            if card not in self.generated_cards:
                self.generated_cards.add(card)
                return card

    # 开户功能
    def open_account(self):
        print("=== 开户操作 ===")
        name = input("请输入姓名：").strip()
        id_card = input("请输入身份证号：").strip()
        phone = input("请输入手机号：").strip()
        balance = float(input("请输入预存金额：").strip())
        password = input("请输入密码：").strip()

        card_num = self.generate_card()
        self.user_data[card_num] = {
            "name": name,
            "id_card": id_card,
            "phone": phone,
            "balance": balance,
            "password": password,
            "status": "正常"  # 状态：正常/锁定
        }
        print(f"开户成功！您的卡号是：{card_num}")

    # 验证卡号和密码（返回：是否验证成功、卡号状态）
    def verify_card_password(self, card_num):
        if card_num not in self.user_data:
            print("卡号不存在！")
            return False, None
        if self.user_data[card_num]["status"] == "锁定":
            print("卡号已被锁定，无法操作！")
            return False, "锁定"

        # 密码验证（最多3次）
        for i in range(3):
            pwd = input("请输入密码：").strip()
            if pwd == self.user_data[card_num]["password"]:
                return True, "正常"
            else:
                print(f"密码错误！剩余{2 - i}次机会")
        # 3次错误，锁定卡号
        self.user_data[card_num]["status"] = "锁定"
        print("密码连续错误3次，卡号已被锁定！")
        return False, "锁定"

    # 查询功能
    def query_balance(self):
        print("=== 查询操作 ===")
        card_num = input("请输入卡号：").strip()
        success, _ = self.verify_card_password(card_num)
        if success:
            print(f"卡号{card_num}的当前余额是：{self.user_data[card_num]['balance']:.2f}元")

    # 取款功能
    def withdraw(self):
        print("=== 取款操作 ===")
        card_num = input("请输入卡号：").strip()
        success, _ = self.verify_card_password(card_num)
        if not success:
            return

        current_balance = self.user_data[card_num]["balance"]
        print(f"当前余额：{current_balance:.2f}元")
        amount = float(input("请输入取款金额：").strip())

        if amount <= 0 or amount > current_balance:
            print("取款金额无效（需大于0且不超过余额）！")
            return
        self.user_data[card_num]["balance"] -= amount
        print(f"取款成功！当前余额：{self.user_data[card_num]['balance']:.2f}元")

    # 存款功能
    def deposit(self):
        print("=== 存款操作 ===")
        card_num = input("请输入卡号：").strip()
        success, _ = self.verify_card_password(card_num)
        if not success:
            return

        current_balance = self.user_data[card_num]["balance"]
        print(f"当前余额：{current_balance:.2f}元")
        amount = float(input("请输入存款金额：").strip())

        if amount <= 0:
            print("存款金额无效（需大于0）！")
            return
        self.user_data[card_num]["balance"] += amount
        print(f"存款成功！当前余额：{self.user_data[card_num]['balance']:.2f}元")

    # 转账功能
    def transfer(self):
        print("=== 转账操作 ===")
        from_card = input("请输入转出卡号：").strip()
        # 验证转出卡
        success, _ = self.verify_card_password(from_card)
        if not success:
            return

        to_card = input("请输入转入卡号：").strip()
        if to_card not in self.user_data:
            print("转入卡号不存在！")
            return
        if self.user_data[to_card]["status"] == "锁定":
            print("转入卡号已被锁定！")
            return

        amount = float(input("请输入转账金额：").strip())
        if amount <= 0 or amount > self.user_data[from_card]["balance"]:
            print("转账金额无效（需大于0且不超过转出卡余额）！")
            return

        # 确认转账
        confirm = input("是否确认转账？(y/n)：").strip().lower()
        if confirm == "y":
            self.user_data[from_card]["balance"] -= amount
            self.user_data[to_card]["balance"] += amount
            print(f"转账成功！转出卡当前余额：{self.user_data[from_card]['balance']:.2f}元")
        else:
            print("已取消转账")

    # 锁定功能
    def lock_card(self):
        print("=== 锁定操作 ===")
        card_num = input("请输入卡号：").strip()
        success, status = self.verify_card_password(card_num)
        if success:
            self.user_data[card_num]["status"] = "锁定"
            print(f"卡号{card_num}已成功锁定！")
        elif status == "正常":
            print("密码验证失败，无法锁定！")

    # 解锁功能
    def unlock_card(self):
        print("=== 解锁操作 ===")
        card_num = input("请输入卡号：").strip()
        if card_num not in self.user_data:
            print("卡号不存在！")
            return
        if self.user_data[card_num]["status"] != "锁定":
            print("卡号当前状态为正常，无需解锁！")
            return

        # 验证密码
        success, _ = self.verify_card_password(card_num)
        if success:
            self.user_data[card_num]["status"] = "正常"
            print(f"卡号{card_num}已成功解锁！")

    # 系统主流程
    def run(self):
        self.show_welcome()
        # 管理员登录
        if not self.admin_login():
            print("登录失败，退出系统……")
            return

        # 功能菜单循环
        while True:
            self.show_menu()
            choice = input("请选择操作（输入对应数字）：").strip()
            if choice == "1":
                self.open_account()
            elif choice == "2":
                self.query_balance()
            elif choice == "3":
                self.withdraw()
            elif choice == "4":
                self.deposit()
            elif choice == "5":
                self.transfer()
            elif choice == "6":
                self.lock_card()
            elif choice == "7":
                self.unlock_card()
            elif choice == "0":
                # 退出需验证管理员信息
                print("请输入管理员账户：", end="")
                account = input().strip()
                print("请输入密码：", end="")
                password = input().strip()
                if account == self.admin_account and password == self.admin_password:
                    print("退出系统成功！")
                    break
                else:
                    print("管理员信息错误，返回功能菜单……")
            else:
                print("无效的选择，请重新输入！")


# 启动系统
if __name__ == "__main__":
    bank = BankSystem()
    bank.run()